package com.example.bgmachine.bgmachine.specifs;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;


import java.util.LinkedList;

public class SpecifAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<SpecifData> mData = new LinkedList<SpecifData>();

    public SpecifAdapter(Context context, LinkedList<SpecifData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        SpecifAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.specif_popup_adapter_gridview_item_content, null);
            convertViewtms= convertView;

            holder = new SpecifAdapter.ViewHolder();
            holder.mSpecifData = mData.get(position);

            holder.specif_type_name_textview = convertViewtms.findViewById(R.id.specif_type_name_textview);
            holder.specif_type_infor_textview = convertViewtms.findViewById(R.id.specif_type_infor_textview);
            holder.specif_type_name_textview.setText( holder.mSpecifData.getTypename() );
            holder.specif_type_infor_textview.setText( holder.mSpecifData.getTypeinfor() );
            convertView.setTag(holder);
        } else {

            holder = (SpecifAdapter.ViewHolder) convertViewtms.getTag();
            holder.mSpecifData = mData.get(position);
            holder.specif_type_name_textview = convertViewtms.findViewById(R.id.specif_type_name_textview);
            holder.specif_type_infor_textview = convertViewtms.findViewById(R.id.specif_type_infor_textview);
            holder.specif_type_name_textview.setText( holder.mSpecifData.getTypename() );
            holder.specif_type_infor_textview.setText( holder.mSpecifData.getTypeinfor() );
        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(SpecifData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, SpecifData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(SpecifData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public  TextView specif_type_name_textview = null;
        public  TextView  specif_type_infor_textview = null;

        public SpecifData mSpecifData = null;
    }

}
